package com.example.SBootDemo.Util.MINA;

import java.util.Date;

import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;

public class TimeServerHandler implements IoHandler {
	 @Override  
	    public void exceptionCaught(IoSession session, Throwable cause)  
	            throws Exception {  
	        cause.printStackTrace();  
	    }  
	  
	    /* 
	     * 这个方法是目前这个类里最主要的， 
	     * 当接收到消息，只要不是quit，就把服务器当前的时间返回给客户端 
	    */  
	    @Override  
	    public void messageReceived(IoSession session, Object message)  
	            throws Exception {  
	        String str = (String)message;  
	        if (str.trim().equalsIgnoreCase("quit")) {  
	            session.close();  
	            return;  
	        }  
	        Date date = new Date();  
	        System.out.println("hello："+str+"信息发送地址"+session.getRemoteAddress()+date.toString());  
	          
	        session.write("i am recived  \n");  
	        System.out.println("Message written...");  
	  
	    }  
	      
	    @Override  
	    public void sessionClosed(IoSession session) throws Exception {  
	        // TODO Auto-generated method stub  
	       
	        System.out.println("客户端与服务端断开连接.....");  
	        
	    }

		@Override
		public void messageSent(IoSession arg0, Object arg1) throws Exception {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void sessionCreated(IoSession arg0) throws Exception {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void sessionIdle(IoSession arg0, IdleStatus arg1) throws Exception {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void sessionOpened(IoSession arg0) throws Exception {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void inputClosed(IoSession arg0) throws Exception {
			// TODO Auto-generated method stub
			
		}  
}
